package importexport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class VCard {
	
	private String firstName;
	private String lastName;
	private String prefix;
	private String title = "other";
	private String phoneNumber = "NA";
	private String email;
	private String altEmail = "NA";
	private String office = "NA";
	private boolean valid = false;
	
	public VCard(String filename) throws IOException {
		File card = new File(filename);
		BufferedReader input = null;
		try {
			input = new BufferedReader(new FileReader(card));
		} catch (FileNotFoundException e) {
			return;
		}
		String line = null;
		while ((line = input.readLine()).equals("")) {
			// do nothing...
		}
		if (!(line.equals("BEGIN:VCARD"))) {
			input.close();
			return;
		}
		while ((line = input.readLine()).equals("")) {
			// do nothing...
		}
		if (!(line.equals("VERSION:2.1"))) {
			input.close();
			return;
		}
		while ((line = input.readLine()) != null) {
			if (line.equals("END:VCARD")) {
				if (!((firstName == null) | (lastName == null) | (email == null) | (title == null))) {
					valid = true;
					if (title.equals("professor") && prefix.equals("")) {
						prefix = "Prof.";
					}
				}
				input.close();
				return;
			}
			else if (line.startsWith("N:")){
				handleName(line.substring(2));
			}
			else if (line.startsWith("FN:")) {
				handleFormattedName(line.substring(3));
			}
			else if (line.startsWith("EMAIL")) {
				handleEmail(line.substring(5));
			}
			else if (line.startsWith("TEL")) {
				handlePhoneNumber(line.substring(3));
			}
			else if (line.startsWith("TITLE")) {
				handleTitle(line.substring(5));
			}
			else if (line.startsWith("OFFICE")) {
				handleOffice(line.substring(6));
			}
		}
		input.close();
	}
	
	private void handleOffice(String line) {
		while (line.indexOf(';') != -1) {
			line = line.substring(line.indexOf(';') + 1);
		}
		if (line.indexOf(':') == -1) {
			return;
		}
		line = line.substring(line.indexOf(':') + 1);
		office = line;
	}
	
	private void handleName(String line) {
		if (line.indexOf(';') == -1) {
			return;
		}
		lastName = line.substring(0, line.indexOf(';'));
		line = line.substring(line.indexOf(';') + 1);
		if (line.indexOf(';') == -1) {
			firstName = line;
			prefix = "";
		}
		else {
			firstName = line.substring(0, line.indexOf(';'));
			line = line.substring(line.indexOf(';') + 1);
			if (line.indexOf(';') == -1) {
				prefix = "";
			}
			else {
				line = line.substring(line.indexOf(';') + 1);
				if (line.indexOf(';') == -1) {
					prefix = line;
				}
				else {
					prefix = line.substring(0, line.indexOf(';'));
				}
			}
		}
		if (!(prefix.equalsIgnoreCase("Prof.") | (prefix.equalsIgnoreCase("Dr.")) | (prefix.equalsIgnoreCase("Ir."))
				| (prefix.equalsIgnoreCase("Prof. Dr.")) | (prefix.equalsIgnoreCase("Prof. Ir."))
				| (prefix.equalsIgnoreCase("Dr. Ir.")) | (prefix.equalsIgnoreCase("Prof. Dr. Ir."))
				| (prefix.equals("")))) {
			prefix = "";
		}
	}
	
	private void handleFormattedName(String line) {
		
	}
	
	private void handleEmail(String line) {
		int pref = 0;
		while (line.indexOf(';') != -1) {
			String temp = line.substring(0, line.indexOf(';'));
			if (temp.equalsIgnoreCase("pref")) {
				pref = 1;
			}
			else if (temp.equalsIgnoreCase("2")) {
				pref = 2;
			}
			line = line.substring(line.indexOf(';') + 1);
		}
		if (line.indexOf(':') == -1) {
			return;
		}
		String temp = line.substring(0, line.indexOf(':'));
		if (temp.equalsIgnoreCase("pref")) {
			pref = 1;
		}
		else if (temp.equalsIgnoreCase("1")) {
			pref = 2;
		}
		line = line.substring(line.indexOf(':') + 1);
		if (pref == 1) {
			email = line;
		}
		else if (pref == 2) {
			altEmail = line;
		}
	}
	
	private void handlePhoneNumber(String line) {
		while (line.indexOf(';') != -1) {
			line = line.substring(line.indexOf(';') + 1);
		}
		if (line.indexOf(':') == -1) {
			return;
		}
		phoneNumber = line.substring(line.indexOf(':') + 1);
	}
	
	private void handleTitle(String line) {
		while (line.indexOf(';') != -1) {
			line = line.substring(line.indexOf(';') + 1);
		}
		if (line.indexOf(':') == -1) {
			return;
		}
		line = line.substring(line.indexOf(':') + 1);
		if (line.equalsIgnoreCase("professor") | line.equalsIgnoreCase("prof") | line.equalsIgnoreCase("prof.")) {
			title = "professor";
		}
		else if (line.equalsIgnoreCase("assistant") | line.equalsIgnoreCase("assistent")) {
			title = "assistant";
		}
		else if (line.equalsIgnoreCase("student")) {
			title = "student";
		}
		else {
			title = "contact";
		}
	}
	
	public boolean isValid() {
		return valid;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String getPrefix() {
		return prefix;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	
	public String getEmail() {
		return email;
	}
	
	public String getAltEmail() {
		return altEmail;
	}
	
	public String getOffice() {
		return office;
	}
}
